package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.GD.CrmIntopiecesDkMapper;
import com.credithc.customers.dataservice.po.GdCustomerProductPO;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.service.OldGdDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

import static com.credithc.customers.dataservice.service.impl.CustomerProductInfoServiceImpl.bulidGdSystemSign;
import static com.credithc.customers.dataservice.service.impl.CustomerProductInfoServiceImpl.getYZTStatus;

@Service
@Slf4j
public class OldGdDataServiceImpl implements OldGdDataService {
    @Value("${old.data.oldInOne}")
    private Integer oldInOne;

    @Autowired
    private CrmIntopiecesDkMapper crmIntopiecesDkMapper;

    @Autowired
    private SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Override
    public void oldData() {
        boolean f =true;
        Long num = crmIntopiecesDkMapper.queryProductMaxId();
        if (num == null){
            return;


        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<GdCustomerProductPO> gdCustomerProductPOS = crmIntopiecesDkMapper.queryDlrProduct(Long.toString(vernier.get()));
                    if (gdCustomerProductPOS.size() > 0) {
                        bulidGDProduct(gdCustomerProductPOS);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidGDProduct(List<GdCustomerProductPO> gdCustomerProductPOS) {
        List<CustomerProductInfoRo> customerProductInfoRos = new ArrayList<>();
        for (GdCustomerProductPO gd:gdCustomerProductPOS) {

            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName("crmdk1");
            try {
                customerProductInfoRo.setProductDeadline(gd.getCarMonth()*30+"");
            } catch (Exception e) {
                log.info("该订单还未签约 无期数字段 "+gd.toString());
            }
            customerProductInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
            customerProductInfoRo.setTableName("crm_intopieces_dk");
            customerProductInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerProductInfoRo.setTableKey(gd.getTableKey());
            customerProductInfoRo.setProductNo(gd.getLoanType()+"");
            customerProductInfoRo.setProductSourceSystem(CustomerConstants.GD+"");
            customerProductInfoRo.setProductAmount(gd.getAccountAmount() == null? gd.getIntopiecesAmount() : gd.getAccountAmount());
            try {
                customerProductInfoRo.setProductEndTime(new Timestamp(gd.getProductEndTime()*1000));
            } catch (Exception e) {
               // e.printStackTrace();
            }
            try {
                customerProductInfoRo.setProductStartTime(new Timestamp(gd.getProductStartTime()*1000));
            } catch (Exception e) {
               // e.printStackTrace();
            }
            customerProductInfoRo.setPreCustomerNo(gd.getPreCustomerNo());
            customerProductInfoRo.setCustomerNo(gd.getCustomerNo());
            customerProductInfoRo.setUserResourceId("PHGD"+gd.getCustomerNo());
            customerProductInfoRo.setProductStatus(gd.getState()==12 ? getYZTStatus("GD"+gd.getState()+gd.getStatus()):getYZTStatus("GD"+gd.getState()));
            bulidGdSystemSign(gd.getIntopiecesSys(),customerProductInfoRo,gd.getAccountSys(),gd.getTrust(),gd.getNewPlatform());
            if (StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())&& !"0".equals(customerProductInfoRo.getCustomerNo())) {
                customerProductInfoRos.add(customerProductInfoRo);
            }
        }
        syncDataService.syncCustomerProduct(customerProductInfoRos);
    }

/*    @Override
    public void oldData() {
        if (oldInOne == null || oldInOne <1){
            oldInOne = 1000;
        }
        boolean f =true;
        int num = crmIntopiecesDkMapper.selectCount(new CrmIntopiecesDkPO());
        if (num < 1){
            return;
        }
        Integer pageNum = num/oldInOne+1 ;
        AtomicReference<Integer> vernier = new AtomicReference<>( pageNum);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1);
                    }
                    RowBounds rowBounds = new RowBounds(vernier.get(),oldInOne);
                    List<GdCustomerProductPO> gdCustomerProductPOS = crmIntopiecesDkMapper.queryGDProduct(rowBounds);
                    if (gdCustomerProductPOS.size() > 0) {
                        bulidGDProduct(gdCustomerProductPOS);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldDateUntil.exit(f, vernier, e);
        }
    }*/

}
